<template>
  <el-dialog
    top="10vh"
    width="800px"
    title="添加/编辑"
    :visible.sync="dialogValue"
    :close-on-click-modal="false"
    append-to-body
  >
    <SubmitForm
      :rule-form="ruleForm"
      :rules="rules"
      :form-list="formList"
      @change="handleFormChange"
      @save="onSave"
      @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import { http } from '@/api'
export default {
  name: 'FormAdd',
  data() {
    return {
      apiKey: 'user',
      dialogValue: false,
      ruleForm: {
      },
      defaultForm: {
        role: 2,
        company_name: '',
        address: '',
        abn: '',
        nickname: '',
        phone: '',
        email: '',
        password: '',
        avatar_url: '',
        status: 1
      },
      formList: [
        // {
        //   key: 'role',
        //   name: 'select',
        //   label: '账号类型',
        //   colSpan: 12,
        //   options: [
        //     {
        //       name: '用户',
        //       value: 1
        //     },
        //     {
        //       name: '代理商',
        //       value: 2
        //     }
        //   ],
        //   role: [1, 2]
        // },
        {
          key: 'company_name',
          name: 'input',
          label: '公司名称',
          colSpan: 24,
        },
        {
          key: 'address',
          name: 'input',
          label: '公司地址',
          type: 'textarea',
          colSpan: 24,
        },
        {
          key: 'abn',
          name: 'input',
          label: 'abn',
          colSpan: 12,
          role: [ 2]
        },
        {
          key: 'nickname',
          name: 'input',
          label: '姓名',
          colSpan: 12,
          role: [1, 2]
        },
        {
          key: 'phone',
          name: 'input',
          label: '手机号',
          colSpan: 12,
          role: [1, 2]
        },
        {
          key: 'email',
          name: 'input',
          label: '邮箱',
          colSpan: 12,
          role: [1, 2]
        },
        {
          key: 'password',
          name: 'input',
          label: '密码',
          type: 'password',
          colSpan: 12,
          role: [1, 2]
        },
        {
          key: 'avatar_url',
          name: 'file',
          multiple: false,
          defaultValue: '',
          label: '头像',
          colSpan: 24,
        },
        {
          key: 'status',
          name: 'switch',
          label: '是否启用',
          activeValue: 1,
          inactiveValue: 0,
          colSpan: 24,
        }
      ]
    }
  },
  computed: {
    // formListShow() {
    //   return this.formList.filter(item => {
    //     console.log('item.role', item.role, this.ruleForm.role)
    //     return item.role.includes(this.ruleForm.role)
    //   })
    // },
    rules() {
      if (this.ruleForm.role === 1) {
        return {
          nickname: [{ required: true, message: '不能为空' }],
          phone: [{ required: true, message: '不能为空' }],
          email: [{ required: true, message: '不能为空' }],
          password: [{ required: true, message: '不能为空' }]
        }
      } else {
        return {
          company_name: [{ required: true, message: '不能为空' }],
          address: [{ required: true, message: '不能为空' }],
          nickname: [{ required: true, message: '不能为空' }],
          phone: [{ required: true, message: '不能为空' }],
          email: [{ required: true, message: '不能为空' }],
          password: [{ required: true, message: '不能为空' }]
        }
      }
    }
  },
  methods: {
    handleFormChange({ key, val }) {

    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, { ...this.ruleForm }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
